from base64 import b64encode, b64decode
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad,unpad
# from Crypto.Random import get_random_bytes
# import json

mode = AES.MODE_CBC

def encrypt(text,key='zoh13i1ojklFGLAKJL:fai~af'):
    key = pad(key.encode('utf-8'), AES.block_size)
    pad_text = pad(text.encode('utf-8'), AES.block_size)
    aes = AES.new(key, mode)
    ct_bytes = aes.encrypt(pad_text)
    iv = b64encode(aes.iv).decode('utf-8')
    ct = b64encode(ct_bytes).decode("utf-8")
    ivct = iv + ',' + ct
    return ivct


def decrypt(text,key='zoh13i1ojklFGLAKJL:fai~af'):
    key = pad(key.encode('utf-8'), AES.block_size)
    text2 = text.split(',')
    iv = b64decode(text2[0])
    pad_text = b64decode(text2[1])
    aes = AES.new(key, mode, iv)
    pt = unpad(aes.decrypt(pad_text), AES.block_size)
    return pt.decode('utf-8')
